﻿Public Class CheckinView

    Private Sub CheckinView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        UI.SetPlaceHolder(txtsearch)
        loademp()
        loadreservtype()
        LoadData()
    End Sub

    Private Sub loademp()
        cboemp.DisplayMember = "EmployeeName"
        cboemp.ValueMember = "EmployeeId"
        cboemp.DataSource = cmd.ExecuteDataTable("SELECT EmployeeId,EmployeeName FROM Employee WHERE Active=1 UNION SELECT 0,'All'")
    End Sub
    Private Sub loadreservtype()
        cbotype.DisplayMember = "ReservationType"
        cbotype.ValueMember = "ReservationTypeId"
        cbotype.DataSource = cmd.ExecuteDataTable("SELECT ReservationTypeId,ReservationType FROM ReservationType WHERE Active=1 UNION SELECT 0,'All'")
    End Sub

    Private Sub LoadData()
        Dim sql = <sql> 
                        SELECT c.*,e.EmployeeName,rt.ReservationType,cl.ClientName
                        FROM CheckIn c 
                        INNER JOIN Employee e ON e.EmployeeId=c.EmployeeId
                        INNer JOIN Client cl on c.ClientId=cl.ClientId
                        INNER JOIN ReservationType rt ON  rt.ReservationTypeId=c.ReservationTypeId
                        WHERE c.Active=1 
                            AND CheckInDate BETWEEN @D1 AND @D2 
                            AND (@EmployeeId=0 OR c.EmployeeId=@EmployeeId ) 
                            AND (@ReTypeId=0 OR c.ReservationTypeId=@ReTypeId) 
                            AND UPPER(CheckInNo) LIKE UPPER(@S);
                 </sql>.Value

        cmd.Parameters("@D1") = Me.dtpstart.Value.Date
        cmd.Parameters("@D2") = Me.dtpend.Value.Date.AddDays(1).AddSeconds(-1)
        cmd.Parameters("@EmployeeId") = Me.cboemp.SelectedValue
        cmd.Parameters("@ReTypeId") = Me.cbotype.SelectedValue
        Cmd.Parameters("@S") = "%" + Me.txtSearch.Text + "%"
        Me.DataGridView1.DataSource = cmd.ExecuteDataTable(sql)

    End Sub

    Private Sub dtpstart_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpstart.ValueChanged
        LoadData()
    End Sub

    Private Sub dtpend_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpend.ValueChanged
        LoadData()
    End Sub

    Private Sub txtsearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsearch.TextChanged
        LoadData()
    End Sub

    Private Sub cboemp_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboemp.SelectedIndexChanged
        LoadData()
    End Sub

    Private Sub cbotype_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbotype.SelectedIndexChanged
        LoadData()
    End Sub

    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        Dim row = DataGridView1.SelectedRows(0)
        Dim id = row.Cells("CheckInId").Value

        cmd.Parameters("@cid") = id
        cmd.ExecuteNonQuery("UPDATE CheckIn SET Active=0 WHERE CheckInId=@cid")

        cmd.Parameters("@cid") = id
        cmd.ExecuteNonQuery("UPDATE CheckInDetail SET Active=0 WHERE CheckInId=@cid")

        cmd.Parameters("@cid") = id
        Dim pid = cmd.ExecuteScalar("SELECT PaymentId FROM Payment Where CheckInId=@cid")
        If pid > 0 Then
            cmd.Parameters("@cid") = id
            cmd.Parameters("@pid") = pid
            cmd.ExecuteNonQuery("UPDATE Payment SET Active=0 WHERE CheckInId=@cid AND PaymentId=@pid")
        End If
        MessageBox.Show("Delete Sucess")
        LoadData()
    End Sub
End Class